
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import action.MessageProcessing;
import bean.DemoBean;

@WebServlet("/DemoController")
public class DemoController extends HttpServlet {
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String pick = request.getParameter("action");

		try {
			if (pick.endsWith("message")) {
				message(request, response);
			}
		} catch (Throwable theException) {
			System.out.println(theException);
		}

	}

	private void message(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		try {
			DemoBean bean = new DemoBean();
			//get data from texts and store in bean
			bean.setName(request.getParameter("ft_author"));
			bean.setMail(request.getParameter("ft_email"));
			bean.setMessage(request.getParameter("ft_message"));
			
			// do something with action MessageProcessing;
			MessageProcessing mp = new MessageProcessing();
			mp.StoreInDB(bean);
			
			// create session
            HttpSession session = request.getSession(true);
            session.setAttribute("message", bean);
            
            //move to message.html
            RequestDispatcher rd = request.getRequestDispatcher("/message/index.html");
            rd.forward(request, response);

			
		} catch (Throwable theException) {
			System.out.println(theException);
		}
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);

	}
}
